package com.doumuxie.frame;

import com.doumuxie.linstener.ChildListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author doumuxie https://gitee.com/doumuxie/
 * @version 1.0
 * @date 2020/9/26 9:02
 * @description 进程状态迁移子窗口
 **/
public class ChildFrame extends JFrame {

    private JButton createBtn;


    public static MyTable tableOne;
    public static DefaultTableModel modelOne;

    private JPopupMenu m_popupMenu;


    /**
     * 表数据
     */
    private String[][] tableData = {};

    /**
     * 表头
     */
    String[] columnNames = {"标识符", "进程名", "优先级", "时间片数", "状态", "数据"};

    public ChildFrame() {
        super("进程状态迁移");
        this.setLayout(null);
        this.setSize(800, 500);
        createBtn = new JButton("随机生成进程");
        createBtn.setBounds(10, 10, 200, 30);
        modelOne = new DefaultTableModel(tableData, columnNames);
        tableOne = new MyTable(modelOne);
        JScrollPane scrollPane = new JScrollPane(tableOne);
        scrollPane.setBounds(10, 50, 760, 400);
        this.add(scrollPane);

        this.createPopupMenu();
        tableOne.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });

        createBtn.addActionListener(new ChildListener(this));
        this.add(createBtn);
    }

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
        mouseRightButtonClick(evt);
    }

    private void mouseRightButtonClick(java.awt.event.MouseEvent evt) {
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            int focusedRowIndex = getTableOne().rowAtPoint(evt.getPoint());
            if (focusedRowIndex == -1) {
                return;
            }
            getTableOne().setRowSelectionInterval(focusedRowIndex, focusedRowIndex);
            m_popupMenu.show(getTableOne(), evt.getX(), evt.getY());
        }

    }


    public MyTable getTableOne() {
        return tableOne;
    }

    public DefaultTableModel getModelOne() {
        return modelOne;
    }

    private JMenuItem blockMenItem;
    private JMenuItem runMenItem;
    private JMenuItem openMenItem;
    private JMenuItem deleteMenItem;

    private void createPopupMenu() {
        m_popupMenu = new JPopupMenu();
        blockMenItem = new JMenuItem();
        runMenItem = new JMenuItem();
        openMenItem = new JMenuItem();
        deleteMenItem = new JMenuItem();
        runMenItem.setText(" 运行  ");
        blockMenItem.setText(" 阻塞  ");
        openMenItem.setText("阻塞唤醒");
        deleteMenItem.setText(" 删除 ");
        blockMenItem.addActionListener(new ChildListener(this));
        runMenItem.addActionListener(new ChildListener(this));
        openMenItem.addActionListener(new ChildListener(this));
        deleteMenItem.addActionListener(new ChildListener(this));
        m_popupMenu.add(runMenItem);
        m_popupMenu.add(blockMenItem);
        m_popupMenu.add(openMenItem);
        m_popupMenu.add(deleteMenItem);
    }

    public JMenuItem getDeleteMenItem() {
        return deleteMenItem;
    }

    public void setDeleteMenItem(JMenuItem deleteMenItem) {
        this.deleteMenItem = deleteMenItem;
    }

    public JMenuItem getOpenMenItem() {
        return openMenItem;
    }

    public void setOpenMenItem(JMenuItem openMenItem) {
        this.openMenItem = openMenItem;
    }

    public JMenuItem getRunMenItem() {
        return runMenItem;
    }

    public void setRunMenItem(JMenuItem runMenItem) {
        this.runMenItem = runMenItem;
    }

    public JPopupMenu getM_popupMenu() {
        return m_popupMenu;
    }

    public void setM_popupMenu(JPopupMenu m_popupMenu) {
        this.m_popupMenu = m_popupMenu;
    }

    public JMenuItem getBlockMenItem() {
        return blockMenItem;
    }

    public void setBlockMenItem(JMenuItem blockMenItem) {
        this.blockMenItem = blockMenItem;
    }

    public JButton getCreateBtn() {
        return createBtn;
    }

    public void setCreateBtn(JButton createBtn) {
        this.createBtn = createBtn;
    }


    public String[][] getTableData() {
        return tableData;
    }

    public void setTableData(String[][] tableData) {
        this.tableData = tableData;
    }

    public String[] getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(String[] columnNames) {
        this.columnNames = columnNames;
    }
}
